package Cinema.Tests;

import static org.junit.Assert.*;

import java.util.Date;
import java.util.Set;

import org.junit.Test;
import org.w3c.dom.Document;

import Utils.XmlParseHelper;

import Cinema.CinemaEntities.Location;
import Cinema.CinemaEntities.Movie;
import Cinema.CinemaEntities.Show;
import Cinema.CinemaEntities.Theater;
import Cinema.DataLayer.XmlBackup;
import Exceptions.BadArgumentException;
import Exceptions.CinemaException;
import Exceptions.DataAccessException;

public class TheaterTest implements IXmlParsableTest{

	/*
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}*/

	final int price = 5;

	@Test
	public void testGetFields_askForFields_FieldsAreInitialized() throws CinemaException {
		Theater testTheater = new Theater("moshe", new Location(79,7979), 10, 15, "haifa");
		assertEquals("haifa", testTheater.getCity());
		assertEquals(79, testTheater.getLocation().getX());
		assertEquals(7979, testTheater.getLocation().getY());
		assertEquals("moshe", testTheater.getName());
		assertEquals(10, testTheater.getRowAmount());
		assertEquals(15, testTheater.getRowSeatsAmount());
	}


	@Test
	public void testConstructor_ConstructWithBadArgs_ExceptionThrowned(){
		@SuppressWarnings("unused")
		Theater t;
		try{
			t = new Theater("moshe", new Location(79,7979), -10, 15, "haifa");
			fail();
		}catch (BadArgumentException e){}
		try{
			t = new Theater("moshe", new Location(79,7979), 10, -15, "haifa");
			fail();
		}catch (BadArgumentException e){}
		try{
			t = new Theater("moshe", new Location(79,7979), 0, 15, "haifa");
			fail();
		}catch (BadArgumentException e){}
		try{
			t = new Theater("moshe", new Location(79,7979), 10, 0, "haifa");
			fail();
		}catch (BadArgumentException e){}
	}


	@Test
	public void testGetShows_tryAddingAshow_showAdded() throws BadArgumentException {
		Theater testTheater = new Theater("moshe", new Location(79,7979), 10, 15, "haifa");
		assertEquals(0, testTheater.getShows().size());
		Show newShow = new Show(new Date(System.currentTimeMillis()+1000), testTheater , new Movie("bamba", "lulu"),price,price);
		testTheater.getShows().add(newShow);
		assertEquals(1, testTheater.getShows().size());
		assertEquals(true, testTheater.getShows().contains(newShow));
	}

	@Test
	public void testGetShows_tryAddingAshowTwice_showAddedOnce() throws BadArgumentException {
		Theater testTheater = new Theater("moshe", new Location(79,7979), 10, 15, "haifa");
		assertEquals(0, testTheater.getShows().size());
		Show newShow = new Show(new Date(System.currentTimeMillis()+1000), testTheater , new Movie("bamba", "lulu"),price,price);
		testTheater.getShows().add(newShow);
		testTheater.getShows().add(newShow);
		assertEquals(1, testTheater.getShows().size());
		assertEquals(true, testTheater.getShows().contains(newShow));
	}

	@Test
	public void testGetShows_TryGetShows_onlyNotExpiredShowsReturn() throws BadArgumentException {
		Theater testTheater = new Theater("moshe", new Location(79,7979), 10, 15, "haifa");
		assertEquals(0, testTheater.getShows().size());
		@SuppressWarnings("deprecation")
		Show OKShow = new Show(new Date(2015,3,3), testTheater , new Movie("bamba", "lulu"),price,price);
		testTheater.getShows().add(OKShow);
		Show expiredShow = new Show(new Date(System.currentTimeMillis()-10), testTheater , new Movie("bamba", "lulu"),price,price);
		testTheater.getShows().add(expiredShow);

		assertEquals(1, testTheater.getShows().size());
		for (Show show : testTheater.getShows()) {
			//only one here
			assertTrue(OKShow==show);
		}
	}

	@SuppressWarnings("deprecation")
	@Test
	@Override
	public void test_initFromXml() throws CinemaException {
		Document testDoc = XmlParseHelper.getDocumentFromFile(".\\tests\\testXml\\TheaterTest2.xml");
		Theater testTheater = new Theater(true, testDoc.getDocumentElement());
		Theater controlTheater = new Theater("vip", new Location(666, 666), 1, 1, "rammalah");

		boolean isEq = controlTheater.checkEquals(testTheater);
		assertTrue(isEq);

		//test with 2 shows in show list
		Document testDoc2 = XmlParseHelper.getDocumentFromFile(".\\tests\\testXml\\TheaterTest3.xml");
		Theater testTheater2 = new Theater(true, testDoc2.getDocumentElement());
		Theater controlTheater2 = new Theater("vip", new Location(666, 666), 1, 1, "rammalah");
		Set<Show> showSet =  controlTheater2.getShows();
		FakeShow show1 = new FakeShow(new Date(111111), controlTheater2, "Operation Grandmother", "comedy",1,2);
		showSet.add(show1);
		show1.setID(7979);
		FakeShow show2 = new FakeShow(new Date(222222), controlTheater2, "Operation Grandmother", "comedy",3,4);
		showSet.add(show2);
		show2.setID(797979);

		boolean isEq2 = controlTheater2.checkEquals(testTheater2);
		assertTrue(isEq2);

		//test alaa xml paesing
		//setting flag to init startup mode
		Document testDoc3 = XmlParseHelper.getDocumentFromFile(".\\tests\\testXml\\TheaterTest4.xml");
		Theater testTheater3 = new Theater(false, testDoc3.getDocumentElement());
		Theater controlTheater3 = new Theater("Cinema City", new Location(21, 66), 43, 101, "Haifa");
		Set<Show> showSet3 =  controlTheater3.getShows();
		showSet3.add(new Show(new Date(2012, 02, 10, 10, 00, 00), controlTheater3, "Movie_0", "Documentary",32,16));
		showSet3.add(new Show(new Date(2012, 02, 11, 10, 00, 00), controlTheater3, "Movie_0", "Documentary",32,16));
		showSet3.add(new Show(new Date(2012, 02, 10, 14, 30, 00), controlTheater3, "Movie_1", "Adventure",53,26));
		showSet3.add(new Show(new Date(2012, 02, 11, 14, 30, 00), controlTheater3, "Movie_1", "Adventure",53,26));

		boolean isEq3 = controlTheater3.checkEquals(testTheater3);
		assertTrue(isEq3);
	}

	@Test
	@Override
	public void test_getXml() throws BadArgumentException, DataAccessException {
		Theater testTheater = new Theater("theater7", new Location(7, 9), 4, 8, "haifa");
		Document testDoc = XmlBackup.createDocFromXmlParsableObject(testTheater);
		Document controlDoc = XmlParseHelper.getDocumentFromFile(".\\tests\\testXml\\TheaterTest.xml");

		boolean isEq = XmlBackupTest.isDocumentsSimilar(controlDoc, testDoc);
		//XmlParserTest.iterateOverDifferences(controlDoc, testDoc);
		assertTrue(isEq);

	}

	private class FakeShow extends Show{


		public FakeShow(Date date, Theater theater, String movieName,
				String movieGenre, int adultPrice, int childPrice) {
			super(date, theater, movieName, movieGenre, adultPrice, childPrice);
		}

		public void setID(long id){
			myID = id;
		}

	}
}
